
package com.itheima.security;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.LoginUseer;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpringSecurityUserService implements UserDetailsService {
    //查找服务，实现查询数据库
    @Autowired
    private UserMapper userMapper;


    //根据用户名查询数据库中用户信息
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        User user = userMapper.loadUserByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        // 查询用户的角色
        List<Role> roles = userMapper.loadRolesByUserId(user.getId());
        user.setRoles(roles);
        System.out.println("登录认证");
        System.out.println(user);
        return new LoginUseer(user);
    }
    }


